class AdminorgController < ApplicationController
    before_filter :login_required
    #模版
    layout "framework"
    
    #首页
    def index
    	@session[:rights] = buildMenu '权限管理'
    end
    ##部门管理
    #增加部门
    def add_department
    	if !request.get?
    		@department = Department.new(params[:department]) 
    		if @department.save
    			flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{@department.name}已经成功增加！</font>"
    			#redirect_to_success("#{@department.name}已经成功增加")
    			redirect_to(:action=>'add_department')
    		end
    	end
    end
    #部门信息列表
    def list_department
    	@departments = Department.find_all
    end
    #修改部门
    def edit_department
    	if request.get?
			@department = Department.find(@params["id"])
		else
			@department = Department.find(@params["id"])
			@department.name = @params["name"]
			@department.memo = @params["memo"]
			if @department.save
				flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{@department.name}已经成功修改！</font>"
				redirect_to(:action=>'list_department')
				#redirect_to_success("#{@department.name}已经成功修改")
			end
		end
    end
    #删除部门
    def del_department
       roleCount = Role.count(["department_id=?",params[:id]])
       if roleCount != 0
           flash[:notice] = "<img src='/images/fail.gif'>&nbsp;<font color='red'>该部门仍然拥有角色,在删除该部门前请首先删除其中的角色！</font>"
       else
    		Department.delete(params[:id])
    		flash[:notice] = "<img src='/images/ok.gif'><font color='green'>删除成功！</font>"
    	end
    	redirect_to(:action=>'list_department')
    end
    ##角色管理
    #增加角色
    def add_role
    	if request.get?
    		@role = Role.new
    		@departments = Department.find_all
    	else
    		@role = Role.new(params[:role]) 
    		if @role.save
    			flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{@role.name}已经成功增加！</font>"
    			redirect_to(:action=>'add_role')
    			#redirect_to_success("#{@role.name}已经成功增加")
    		end
    	end
    end
    #角色信息列表
    def list_role
        @roles = Role.find(:all,:order=>"department_id DESC")
    end
    
    def showUpdateRole
        @role = Role.find(@params["id"])
        @departments = Department.find_all
    end
    #修改角色
    def edit_role
    	if request.get?
			@role = Role.find(@params["id"])
			@departments = Department.find_all
		else
			role = Role.update(@params["id"],:name=>@params["name"],:department_id=>@params["department_id"],:memo=>@params["memo"])
			if role
				flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{role.name}已经成功修改！</font>"
    			redirect_to(:action=>'list_role')
				#redirect_to_success("#{role.name}已经成功修改")
			end
		end
    end
	#删除角色
    def del_role
       accountCount = Account.count(["role_id=?",params[:id]])
       if accountCount != 0
           flash[:notice] = "<img src='/images/fail.gif'>&nbsp;<font color='red'>该角色仍然拥有员工,在删除该角色前请首先删除其中的员工！</font>"
       else
    		Role.delete(params[:id])
    		flash[:notice] = "<img src='/images/ok.gif'><font color='green'>删除成功！</font>"
    	end
    	redirect_to(:action=>'list_role')
    end
    ##员工管理
    #增加员工
    def add_employee
    	@roles = Role.find_all
    	if request.get?
    		@account = Account.new
    	else
    		@employee = Employee.new(params[:employee])
    		@employee.state = 1
    		@employee.save
    		@account = @employee.create_account(params[:account])
    		if @account.id && @account.id!=0
    			@account.login = @account.id
    			@account.save
    			flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{@employee.name}已经成功增加！该员工登录ID为#{formatSaleNum(@account.id,4)}</font>"
    		else
    			Employee.delete @employee.id
    		end
    	end
    end
    #员工信息列表
    def list_employee
    	@departments = Department.find_all
    	if request.get?
    		state = 1
    		if params[:state]=='0'
    			state = 0
                        #flash[:notice] = "<img src='/images/ok.gif'><font color='green'>\"#{state}\"</font>"
    		end 
    		@employee_pages, @employees = paginate(:employees,:conditions=>"state = #{state}")
    		#@employee_pages, @employees = paginate(:employees)
    	else
    	    department_id = params[:department_id]
    	    employee_name = params[:employee_name]
    	    #conditions = 'state = 1'
    	    joins      = nil
    	    if employee_name != nil && employee_name != ''
    	    	#conditions += " and name='#{employee_name}'"
    	    	conditions = "name like '%#{employee_name}%'"
    	    end
    	    @employee_pages, @employees = paginate(:employees,:conditions=>conditions)
    	    if department_id != '-1'
    	    	tmp_roles = Role.find_by_sql("select roles.id from roles,departments where roles.department_id = departments.id and departments.id=#{department_id}")
    	    	@sel_roles = []
    	    	tmp_roles.each {|r| @sel_roles<< r.id}
    	    end
    	end
    end
    #停用员工
    def stop_employee
    	employee = Employee.find(params[:id])
    	employee.state = 0
    	if employee.save
    		redirect_to(:action=>'list_employee')
    	end
    end
    #启用员工
    def start_employee
    	employee = Employee.find(params[:id])
    	employee.state = 1
    	if employee.save
    		redirect_to(:action=>'list_employee')
    	end
    end
    #显示员工具体信息
    def show_employee
    	@employee = Employee.find(params[:id])
    end
    #修改员工
    def edit_employee
    	retInfo = nil
    	@employee_id = @params["id"]
    	@type = params[:type]
        if params[:married] == nil
                params[:married] = 0
        end
    	if request.get?
    		@roles = Role.find_all
    		if !'self'.eql? params[:type]
    			show_employee
    		else
    			@employee = @session[:account].employee
    		end
    	else
    		employee = Employee.update(@params["employee_id"],:name=>params[:name],:photo=>params[:photo],
    		:english_name=>params[:english_name],:sex=>params[:sex],:married=>params[:married],:birthday_at=>params[:date],
    		:phone=>params[:phone],:ori_phone=>params[:ori_phone],:mobile=>params[:mobile],:lingtong=>params[:lingtong],
    		:email=>params[:email],:resume=>params[:resume])
    		if params[:employee][:photo].size != 0
    			employee.photo=params[:employee][:photo]
    			employee.save
    		end
    		account = employee.account
    		if account
    			account.role_id = params[:role_id]
    			puts 
    			if params[:account][:password] and !params[:account][:password].eql? '' 
    				account.password = params[:account][:password] 
    			end
    			account.update
    		end
    		flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{employee.name}的信息已经成功修改！</font>"
    		if !'self'.eql? params[:type]		
    			redirect_to(:action=>'list_employee')
    		else
    			@employee = @session[:account].employee
    			@employee_id = @employee.id
    		end
    	end
    end
    ##权限分配
    #增加频道
    def add_right
    	if !request.get?
    		@right = Right.new(params[:right]) 
    		if @right.save
    			flash[:notice] = "<img src='/images/ok.gif'><font color='green'>\"#{@right.name}\"已经成功增加！</font>"
    			redirect_to(:action=>'add_right')
    		end
    	end
    end
    #频道信息列表
    def list_right
    	@rights = Right.find_all
    	@right_pages, @rights = paginate :right, :conditions=>'ismenu is null',:order=>'id',:per_page => 20
    end
    #修改频道
    def edit_right
    	if request.get?
			@right = Right.find(@params["id"])
		else
			@right = Right.find(@params["id"])
			@right.name = @params["name"]
			@right.url = @params["url"]
			@right.mgroup = @params["mgroup"]
			@right.module = @params["module"]
			if @right.save
				flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{@right.name}已经成功修改！</font>"
				redirect_to(:action=>'list_right')
				#redirect_to_success("#{@department.name}已经成功修改")
			end
		end
    end
    #删除频道
    def del_right
    	Right.delete(params[:id])
    	flash[:notice] = "<img src='/images/ok.gif'><font color='green'>删除成功！</font>"
    	redirect_to(:action=>'list_right')
    end
    #分配权限
    def assign_right
    	if !request.get?
    		addArray = Array.new
    		delArray = Array.new
    		roleId = params[:hidRole]
    		params[:hidAdd].each(',') do |add|
    			right = Right.find(:first,:conditions=>"module='#{params[:hidProv]}' and name='#{add.delete(',')}'")
    			rightRole = RightRole.new
    			rightRole.right_id = right.id
    			rightRole.role_id = roleId
    			rightRole.save
    		end
    		params[:hidDel].each(',') do |del|
    			right = Right.find(:first,:conditions=>"module='#{params[:hidProv]}' and name='#{del.delete(',')}'")
    			RightRole.delete_all "role_id=#{roleId} and right_id=#{right.id}"
    		end
    		flash[:notice] = "<img src='/images/ok.gif'><font color='green'>权限分配已经完成！</font>"
    	end
    	
    	@roles   = Role.find_all
    	rights = nil
    	@roleId=params[:roleId]
    	if @roleId and !@roleId.eql? ''
    		rights = (Role.find @roleId).rights
    	else
    		rights = @roles[0].rights
    	end
    	allrights = Right.find(:all,:conditions=>'ismenu is null',:order=>'id')
    	@sReady = ''
        @sSelect = ''
    	currentModule = ''
    	isSellected = false
    	
        #debugger
    	allrights.each do |allright|
    		rights.each do |right|
    			if allright.id == right.id
    				isSellected = true
    				break 
    			end
    		end
    		if !allright.module.eql? currentModule
    			if isSellected
    				@sSelect += ';' if !currentModule.eql? ''
    				@sReady += ';' if !currentModule.eql? ''
    				@sSelect += allright.name
    			else
    				@sReady += ';' if !currentModule.eql? ''
    				@sSelect += ';' if !currentModule.eql? ''
    				@sReady += allright.name
    			end
    		else
    			if isSellected
    				#如果最后一个不等于;则加个,
    				@sSelect += ',' if @sSelect[@sSelect.length-1]!=59
    				@sSelect += allright.name
    			else
    				@sReady += ',' if @sReady[@sReady.length-1]!=59
    				@sReady += allright.name
    			end
    		end
    		currentModule = allright.module
    		isSellected = false
    	end
    end
end
